package socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public  class SocketManager {
    public void serverSocket() throws IOException, InterruptedException {
        Socket accept = null;
        //1、构建socket服务
        try{
            ServerSocket serverSocket = new ServerSocket(6000);
            System.out.println("监听程序启动...");
//            1、建立一个连接池
            ExecutorService pool = Executors.newFixedThreadPool(15);

            while((accept = serverSocket.accept())!=null){      //接收连接请求, 阻塞程序
//                2、创建一个个任务对象
                Socket finalAccept = accept;
                MyThread run = new MyThread(finalAccept);
//                3、将任务对象放入连接池
                pool.execute(run);
                MyThread.set.add(run);
            }

        }catch (Exception e){

        }

    }

    public static void main(String[] args) {
        try {
            new SocketManager().serverSocket();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
